#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P3^5;
sbit RW=P3^6;
sbit E=P3^4;
sbit s1=P1^0;
sbit s2=P1^1;
sbit s3=P1^2;
sbit di=P2^3;
void yanshi(uchar xms);
void writecommand(uchar x);
void writedate(uchar x);
void xianshi(uchar add,uchar date); 
void jianpansaomiao();
void beep();
void init();
uchar miao=0,fen=0,shi=0,t=0,num,slnum=0;
uchar code shuzu[]="2016-3-13 03";
void main()
	{
		init();
		while(1)
			{
			jianpansaomiao();
			}	
	}
void yanshi(uchar xms)
	{
		uchar i,j;
		for(i=xms;i>0;i--)
			for(j=110;j>0;j--);
	}
void writecommand(uchar x)
	{
		RS=0;
		E=0;
		P0=x;
		yanshi(5);
		E=1;
		yanshi(5);
		E=0;
	}
void writedate(uchar x)
	{
		RS=1;
		E=0;
		P0=x;
		yanshi(5);
		E=1;
		yanshi(5);
		E=0;
		
	}
void xianshi(uchar add,uchar date)
	{
		uchar shi,ge;
		shi=date/10;
		ge=date%10;
		writecommand(0x80+0x40+add);
		writedate(0x30+shi);
		writedate(0x30+ge);
	}
void beep()
	{n 
		di=0;
		yanshi(100);
		di=1;
	}
void init()
	{
		RW=0;
		E=0;
		writecommand(0x38);
		writecommand(0x0c);
		writecommand(0x06);
		writecommand(0x01);
		writecommand(0x80);
		for(num=0;num<12;num++)
		{
				writedate(shuzu[num]);
				yanshi(5);
		}
		writecommand(0x80+0x40+6);
		writedate(';');
		yanshi(5);
		writecommand(0x80+0x40+9);
		writedate(';');
		yanshi(5);
		xianshi(4,shi);
		xianshi(7,fen);
		xianshi(10,miao);
		TMOD=0x01;
		TH0=(65536-50000)/256;
		TL0=(65536-50000)%256;
		EA=1;
		ET0=1;
		TR0=1;
	}
void timer0() interrupt 1
	{
		TH0=(65536-50000)/256;
		TL0=(65536-50000)%256;
		t++;
		if(t==20)
		{
			t=0;
			miao++;
			if(miao==60)
			{
				miao=0;
				fen++;
				if(fen==60)
				{
					fen=0;
					shi++;
					if(shi==24)
					{
						shi=0;
					}
					xianshi(4,shi);
				}
				xianshi(7,fen);
			}
			xianshi(10,miao);	
		}
	}
void jianpansaomiao()
{
		if(s1==0)
		{
			yanshi(5);
			if(s1==0)
			{
				slnum++;
				while(!s1);
				beep();
				if(slnum==1)
				{
					TR0=0;
					writecommand(0x80+0x40+8);
					writecommand(0x0f);
				}
				if(slnum==2)
				{
					writecommand(0x80+0x40+8);
				}
				if(slnum==3)
				{
					writecommand(0x80+0x40+5);
				}
				if(slnum==4)
				{
					slnum=0;
					writecommand(0x0c);
				}
			}
		}
		if(slnum!=0)
		{
			if(s2==0)
			{
				yanshi(5);
				if(s2==0)
				{
					while(!s2);
					beep();
					if(slnum==1)
					{
						miao++;
						if(miao==60)
						miao=0;
						xianshi(10,miao);
						writecommand(0x80+0x40+10);	
					}
					if(slnum==2)
					{
						fen++;
						if(fen==60)
						fen=0;
						xianshi(7,fen);
						writecommand(0x80+0x40+7);
					}
					if(slnum==3)
					{
						shi++;
						if(shi==24)
						shi=0;
						xianshi(4,shi);
						writecommand(0x80+0x40+4);
					}
				}
			}
			
		if(s3==0)
		{
			yanshi(5);
			if(s3==0)
			{
				while(!s3);
				beep();
				if(slnum==1)
				{
					miao--;
					if(miao==-1)
					miao=59;
					xianshi(10,miao);
					writecommand(0x80+0x40+10);
				}
				if(slnum==2)
				{
					fen--;
					if(fen==-1)
					fen=59;
					xianshi(7,fen);
					writecommand(0x80+0x40+7);
				}
				if(slnum==3)
				{

					shi--;
					if(shi==-1)
					shi=23;
					xianshi(4,shi);
					writecommand(0x80+0x40+4);
				}
			}
		}
	}
}


